package br.edu.ufcg.splab.techniques.reduction.heuristics;

import static org.junit.Assert.assertTrue;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import br.edu.ufcg.splab.core.InterfaceGenerate;
import br.edu.ufcg.splab.core.InterfaceGraph;
import br.edu.ufcg.splab.core.graph.Tree;
import br.edu.ufcg.splab.exceptions.LTSBTException;
import br.edu.ufcg.splab.generator.ConvertGraphToTree;
import br.edu.ufcg.splab.generator.TestCase;
import br.edu.ufcg.splab.generator.TestCaseGenerator;
import br.edu.ufcg.splab.generator.TestSuite;
import br.edu.ufcg.splab.parser.GraphToAnnotatedGraphParser;
import br.edu.ufcg.splab.techniques.reduction.requirement.RequirementTracer;
import br.edu.ufcg.splab.techniques.reduction.requirement.TransitionPairsTracerGenerator;
import br.edu.ufcg.splab.util.Controller;
import br.edu.ufcg.splab.util.ReadTGF;

public class GreedyEssentialTest {
	
	@Test
	public void testReduceTestSuite() {
		try{
			String tgf = "./test-data/reduction/graph_test_reduction.tgf";
			
			ReadTGF parser = new ReadTGF();			
			InterfaceGraph graph = parser.getGraph(tgf);
			
			InterfaceGenerate generator = new TestCaseGenerator();
			ConvertGraphToTree convertsGraphToTree = new ConvertGraphToTree();
			Tree tree = convertsGraphToTree.converterGraph(graph);
			tree.expand(1);
			TestSuite testSuite = generator.generate(tree);			
			TransitionPairsTracerGenerator pairs = new TransitionPairsTracerGenerator();
			RequirementTracer tracer = pairs.getTracer(testSuite);
			List<RequirementTracer> tracers = new ArrayList<RequirementTracer>();
			tracers.add(tracer);
			
			GreedyEssential ge = new GreedyEssential();
			TestSuite testSuiteReduced = ge.reduceTestSuite(testSuite, tracers);
			
			assertTrue(tracer.getEssentialTestCases().size() == 1);
			assertTrue(testSuiteReduced.containsAll(tracer.getEssentialTestCases()));
			
			for(TestCase tc: testSuiteReduced){
				tracer.getRequirements().removeAll(tracer.getRequirementsSatisfied(tc));
			}
			assertTrue(tracer.getRequirements().isEmpty());
		}catch (LTSBTException e){
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testReduceTestSuite2() {
		try{
			String tgf = "./test-data/reduction/annotated_graph.tgf";
			
			ReadTGF read = new ReadTGF();			
			InterfaceGraph graph = read.getGraph(tgf);				
			GraphToAnnotatedGraphParser parser = new GraphToAnnotatedGraphParser();
			InterfaceGraph annotatedGraph = parser.graphContraction(graph);
			
			InterfaceGenerate generator = new TestCaseGenerator();
			ConvertGraphToTree convertsGraphToTree = new ConvertGraphToTree();
			Tree tree = convertsGraphToTree.converterGraph(annotatedGraph);
			tree.expand(1);
			TestSuite testSuite = generator.generate(tree);	
			
			Controller controller = new Controller();
			TestSuite validTS = controller.getValidTS(testSuite);
			
			TransitionPairsTracerGenerator pairs = new TransitionPairsTracerGenerator();
			RequirementTracer tracer = pairs.getTracer(validTS);
			List<RequirementTracer> tracers = new ArrayList<RequirementTracer>();
			tracers.add(tracer);
			GreedyEssential ge = new GreedyEssential();
			TestSuite testSuiteReduced = ge.reduceTestSuite(validTS, tracers);
						
			for(TestCase tc: testSuiteReduced){
				tracer.getRequirements().removeAll(tracer.getRequirementsSatisfied(tc));
			}
			System.out.println("tracer.getRequirements(): " + tracer.getRequirements().size());
			assertTrue(tracer.getRequirements().isEmpty());
		}catch (LTSBTException e){
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}
